home *** CD-ROM | disk | FTP | other *** search
/ Total Network Tools 2002 / NextStepPublishing-TotalNetworkTools2002-Win95.iso / Archive / Misc Servers / Zope.exe / VSEVAL.PY < prev    next >
Encoding:
Python Source  |  1999-10-28  |  7.5 KB  |  216 lines

  1. ##############################################################################
  2. # Zope Public License (ZPL) Version 1.0
  3. # -------------------------------------
  4. # Copyright (c) Digital Creations.  All rights reserved.
  5. # This license has been certified as Open Source(tm).
  6. # Redistribution and use in source and binary forms, with or without
  7. # modification, are permitted provided that the following conditions are
  8. # met:
  9. # 1. Redistributions in source code must retain the above copyright
  10. #    notice, this list of conditions, and the following disclaimer.
  11. # 2. Redistributions in binary form must reproduce the above copyright
  12. #    notice, this list of conditions, and the following disclaimer in
  13. #    the documentation and/or other materials provided with the
  14. #    distribution.
  15. # 3. Digital Creations requests that attribution be given to Zope
  16. #    in any manner possible. Zope includes a "Powered by Zope"
  17. #    button that is installed by default. While it is not a license
  18. #    violation to remove this button, it is requested that the
  19. #    attribution remain. A significant investment has been put
  20. #    into Zope, and this effort will continue if the Zope community
  21. #    continues to grow. This is one way to assure that growth.
  22. # 4. All advertising materials and documentation mentioning
  23. #    features derived from or use of this software must display
  24. #    the following acknowledgement:
  25. #      "This product includes software developed by Digital Creations
  26. #      for use in the Z Object Publishing Environment
  27. #      (http://www.zope.org/)."
  28. #    In the event that the product being advertised includes an
  29. #    intact Zope distribution (with copyright and license included)
  30. #    then this clause is waived.
  31. # 5. Names associated with Zope or Digital Creations must not be used to
  32. #    endorse or promote products derived from this software without
  33. #    prior written permission from Digital Creations.
  34. # 6. Modified redistributions of any form whatsoever must retain
  35. #    the following acknowledgment:
  36. #      "This product includes software developed by Digital Creations
  37. #      for use in the Z Object Publishing Environment
  38. #      (http://www.zope.org/)."
  39. #    Intact (re-)distributions of any official Zope release do not
  40. #    require an external acknowledgement.
  41. # 7. Modifications are encouraged but must be packaged separately as
  42. #    patches to official Zope releases.  Distributions that do not
  43. #    clearly separate the patches from the original work must be clearly
  44. #    labeled as unofficial distributions.  Modifications which do not
  45. #    carry the name Zope may be packaged in any form, as long as they
  46. #    conform to all of the clauses above.
  47. # Disclaimer
  48. #   THIS SOFTWARE IS PROVIDED BY DIGITAL CREATIONS ``AS IS'' AND ANY
  49. #   EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  50. #   IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  51. #   PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL DIGITAL CREATIONS OR ITS
  52. #   CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  53. #   SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  54. #   LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
  55. #   USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  56. #   ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  57. #   OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
  58. #   OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  59. #   SUCH DAMAGE.
  60. # This software consists of contributions made by Digital Creations and
  61. # many individuals on behalf of Digital Creations.  Specific
  62. # attributions are listed in the accompanying credits file.
  63. ##############################################################################
  64. """Very Safe Python Expressions
  65. """
  66. __rcs_id__='$Id: VSEval.py,v 1.23 1999/10/28 18:02:41 brian Exp $'
  67. __version__='$Revision: 1.23 $'[11:-2]
  68.  
  69. from string import translate, strip
  70. import string
  71. gparse=None
  72.  
  73. nltosp=string.maketrans('\r\n','  ')
  74.  
  75. def default_slicer(env, s, *ind):
  76.     l=len(ind)
  77.     if l==2: return s[ind[0]:ind[1]]
  78.     elif l==1: return s[ind[0]:]
  79.     return s[:]
  80.  
  81. def careful_mul(env, *factors):
  82.     # r = result (product of all factors)
  83.     # c = count (product of all non-sequence factors)
  84.     # s flags whether any of the factors is a sequence
  85.     r=c=1
  86.     s=None
  87.     for factor in factors:
  88.         try:
  89.             l=len(factor)
  90.             s=1
  91.         except TypeError:
  92.             c=c*factor
  93.         if s and c > 1000:
  94.             raise TypeError, \
  95.                   'Illegal sequence repeat (too many repetitions: %d)' % c
  96.         r=r*factor
  97.     return r
  98.  
  99.  
  100. default_globals={
  101.     '__builtins__':{},
  102.     '__guarded_mul__':       careful_mul,
  103.     '__guarded_getattr__':   lambda env, inst, name: getattr(inst, name),
  104.     '__guarded_getitem__':   lambda env, coll, key:  coll[key],
  105.     '__guarded_getslice__':  default_slicer,
  106.     }
  107.  
  108.  
  109.  
  110. class Eval:
  111.     """Provide a very-safe environment for evaluating expressions
  112.  
  113.     This class lets you overide operations, __power__, __mul__,
  114.     __div__, __mod__, __add__, __sub__, __getitem__, __lshift__,
  115.     __rshift__, __and__, __xor__, __or__,__pos__, __neg__, __not__,
  116.     __repr__, __invert__, and __getattr__.
  117.  
  118.     For example, __mult__ might be overridden to prevent expressions like::
  119.  
  120.       'I like spam' * 100000000
  121.  
  122.     or to disallow or limit attribute access.
  123.  
  124.     """
  125.  
  126.     def __init__(self, expr, globals=default_globals):
  127.         """Create a 'safe' expression
  128.  
  129.         where:
  130.  
  131.           expr -- a string containing the expression to be evaluated.
  132.  
  133.           globals -- A global namespace.
  134.         """
  135.         global gparse
  136.         if gparse is None: import gparse
  137.  
  138.         expr=strip(expr)
  139.         
  140.         self.__name__=expr
  141.         expr=translate(expr,nltosp)
  142.         self.expr=expr
  143.         self.globals=globals
  144.  
  145.         co=compile(expr,'<string>','eval')
  146.  
  147.         names=list(co.co_names)
  148.  
  149.         # Check for valid names, disallowing names that begin with '_' or
  150.         # 'manage'. This is a DC specific rule and probably needs to be
  151.         # made customizable!
  152.         for name in names:
  153.             if name[:1]=='_' and name not in ('_', '_vars', '_getattr'):
  154.                 raise TypeError, 'illegal name used in expression'
  155.                 
  156.         used={}
  157.  
  158.         i=0
  159.         code=co.co_code
  160.         l=len(code)
  161.         LOAD_NAME=101   
  162.         HAVE_ARGUMENT=90        
  163.         def HAS_ARG(op): ((op) >= HAVE_ARGUMENT)
  164.         while(i < l):
  165.             c=ord(code[i])
  166.             if c==LOAD_NAME:
  167.                 name=names[ord(code[i+1])+256*ord(code[i+2])]
  168.                 used[name]=1
  169.                 i=i+3           
  170.             elif c >= HAVE_ARGUMENT: i=i+3
  171.             else: i=i+1
  172.         
  173.         self.code=gparse.compile(expr,'<string>','eval')
  174.         self.used=tuple(used.keys())
  175.  
  176.     def eval(self, mapping):
  177.         d={'_vars': mapping}
  178.         code=self.code
  179.         globals=self.globals
  180.         for name in self.used:
  181.             try: d[name]=mapping.getitem(name,0)
  182.             except KeyError:
  183.                 if name=='_getattr':
  184.                     d['__builtins__']=globals
  185.                     exec compiled_getattr in d
  186.  
  187.         return eval(code,globals,d)
  188.  
  189.     def __call__(self, **kw):
  190.         return eval(self.code, self.globals, kw)
  191.  
  192. compiled_getattr=compile(
  193.     'def _getattr(o,n): return __guarded_getattr__(_vars,o,n)',
  194.     '<string>','exec')
  195.